package edu.xmut.kakamusic.fragment.chart;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import java.util.ArrayList;

import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.viewmodel.PieViewModel;

public class PieFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_pie, container, false);
        PieChart chart = root.findViewById(R.id.pieChart);
        PieViewModel pieViewModel = new ViewModelProvider(this).get(PieViewModel.class);
        pieViewModel.getPieList().observe(getViewLifecycleOwner(), pies -> {
            ArrayList<PieEntry> pieEntries = new ArrayList<>();
            for (int i = 0; i < pies.size(); i++) {
                pieEntries.add(new PieEntry(pies.get(i).getPercentage(), pies.get(i).getSalaries()));
            }
            PieDataSet pieDataSet = new PieDataSet(pieEntries, "工资占比");
            pieDataSet.setColors(Color.parseColor("#DAB8D1"),
                    Color.parseColor("#B59EBA"),
                    Color.parseColor("#8D85AA"),
                    Color.parseColor("#636E9A"));
            pieDataSet.setValueTextColor(Color.parseColor("#FE8D9B"));
            pieDataSet.setValueTextSize(12f);
            pieDataSet.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    return value + "%";
                }
            });
            PieData pieData = new PieData(pieDataSet);
            chart.setData(pieData);
            chart.setCenterText("点击显示\n相关数据");
            chart.setCenterTextColor(Color.BLACK);
            chart.setCenterTextSize(24f);
            chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
                @Override
                public void onValueSelected(Entry e, Highlight h) {
                    chart.setCenterText(((PieEntry) e).getLabel() + "\n" + ((PieEntry) e).getValue() + "%");
                }

                @Override
                public void onNothingSelected() {
                    chart.setCenterText("点击显示\n相关数据");
                }
            });
            chart.setExtraTopOffset(10f);
            chart.invalidate();
            Legend l = chart.getLegend();
            l.setFormSize(10f);
            l.setForm(Legend.LegendForm.CIRCLE);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            Description description = chart.getDescription();
            description.setText("Android工程师经验与工资占比情况");
            description.setTextAlign(Paint.Align.CENTER);
            description.setPosition(540f, 100f);
            description.setTextSize(16f);
            description.setTextColor(Color.BLUE);
            chart.animateXY(3000, 3000);

        });

        return root;
    }
}
